package com.grantha.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.grantha.util.DB;

/**
 * Servlet implementation class AddUserController
 */
@WebServlet("/IssuedBooksServlet")
public class IssuedBooksServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	Logger log = Logger.getLogger(this.getClass());
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out  =  response.getWriter();
		try {
			log.info("test");
			//log.error("hello");
			String issuedate= request.getParameter("issuedate"); 
			log.info("issud on="+issuedate);
			
			ResultSet rs = DB.search("select * from dailyissueregister where dateofissue=?", issuedate);
			
			while (rs.next()) {
				out.println(rs.getString("bookno"));
				out.println(rs.getString("patno"));
				out.println(rs.getString("dateofissue"));
				out.println(rs.getString("dateofreturn"));
				out.println(rs.getString("returned"));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

	/*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		
		log.info("Servlet Stated");
		//coding start 
		
		String issuedate= request.getParameter("issuedate"); 
		//String password = request.getParameter("password");
		//String rights = request.getParameter("rights");
		
		log.info("issud on="+issuedate);
		//log.info("password="+password);
		//log.info("rights="+rights);
		
		
		try {
			
			//DB.update("insert into users (username, password, rights) values (?, ?, ?)", username, password, rights );
			ResultSet rs = DB.search("select * from dailyissueregister", null);
			out.print("success");
			while (rs.next()) {
				out.println(rs.getString("bookno"));
				log.info("1:"+rs.getString("bookno"));
				out.println(rs.getString("patno"));
				out.println(rs.getString("dateofissue"));
				out.println(rs.getString("dateofreturn"));
				out.println(rs.getString("returned"));
			}
			log.info("success");
		} catch (Exception e) {
			out.print("error");
			log.error("Error");
		}
		
		//coding ends
		
		out.close();
		log.info("Servlet end");
	}

}*/
